package com.fan.shop.businesslogic;

import java.math.BigDecimal;

import org.apache.log4j.Logger;

import com.fan.shop.domain.PurchaseLine;
import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.PurchaseLineService;

public class PurchaseOrderLineUpdateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(PurchaseOrderLineUpdateProcess.class);
	private PurchaseLineService purchaseLineService;
	private String poNumber;
	private String itemCode;
	private BigDecimal price;
	private int qty;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PurchaseOrderLineUpdateProcess.logger = logger;
	}

	public PurchaseLineService getPurchaseLineService() {
		return purchaseLineService;
	}

	public void setPurchaseLineService(PurchaseLineService purchaseLineService) {
		this.purchaseLineService = purchaseLineService;
	}

	public String getPoNumber() {
		return poNumber;
	}

	public void setPoNumber(String poNumber) {
		this.poNumber = poNumber;
	}

	public String getItemCode() {
		return itemCode;
	}

	public void setItemCode(String itemCode) {
		this.itemCode = itemCode;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public void setPrice(BigDecimal price) {
		this.price = price;
	}

	public int getQty() {
		return qty;
	}

	public void setQty(int qty) {
		this.qty = qty;
	}

	@Override
	public Integer processImpl() {
		if (purchaseLineService.hasPurchaseLine(poNumber, itemCode)) {
			PurchaseLine purchaseLine = purchaseLineService
					.getPurchaseLine(poNumber, itemCode);
			purchaseLine.setPrice(price);
			purchaseLine.setQuantity(qty);
			try{
				purchaseLineService.updatePurchaseLine(purchaseLine);
			}
			catch(Exception e)
			{
				setErrorMessage(e.getMessage());
				e.printStackTrace();
				return ShopGlobalVariables.FAILED;
			}
			return ShopGlobalVariables.SUCCESS;
		}
		else
		{
			setErrorMessage("Purchase Order Line: " + poNumber + " " + itemCode + " doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
	}

}
